//
//  ViewController.m
//  viewTest
//
//  Created by 俞卫忠 on 16/8/16.
//  Copyright © 2016年 YuWeiZhong. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic) UIView *v_navTitle;
@property (nonatomic) UIView *v_line;

- (IBAction)btn_click:(id)sender;

@end

typedef enum : NSUInteger {
    Fade = 1,                   //淡入淡出
    Push,                       //推挤
    Reveal,                     //揭开
    MoveIn,                     //覆盖
    Cube,                       //立方体
    SuckEffect,                 //吮吸
    OglFlip,                    //翻转
    RippleEffect,               //波纹
    PageCurl,                   //翻页
    PageUnCurl,                 //反翻页
    CameraIrisHollowOpen,       //开镜头
    CameraIrisHollowClose,      //关镜头
    CurlDown,                   //下翻页
    CurlUp,                     //上翻页
    FlipFromLeft,               //左翻转
    FlipFromRight,              //右翻转
    
} AnimationType;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupAllTitle];
//    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"02.jpg"]];

    [self drawRect];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 添加标题
- (void)setupAllTitle
{
    self.v_navTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    self.v_navTitle.center = CGPointMake([[UIScreen mainScreen] bounds].size.width / 2, [[UIScreen mainScreen] bounds].size.height / 2-200);
    self.v_navTitle.backgroundColor = [UIColor redColor];
    self.v_line = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 100, 4)];
    _v_line.backgroundColor = [UIColor blackColor];
    [self.v_navTitle addSubview:_v_line];
    [self.view addSubview:self.v_navTitle];
    
    NSUInteger count = 2;
    CGFloat btnW = 100;
    CGFloat btnX = 40;
    CGFloat btnH = 44;
    for (int i = 0; i < count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        
//        UIViewController *vc = self.childViewControllers[i];
        [titleButton setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
        // 设置标题颜色
        [titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        // 设置标题字体
        titleButton.titleLabel.font = [UIFont systemFontOfSize:18];
        
        btnX = i * btnW;
        
        titleButton.frame = CGRectMake(btnX, 0, btnW, btnH);
        
        // 监听按钮点击
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_v_navTitle addSubview:titleButton];
        
    }
    
}

#pragma mark - 点击标题

- (void)titleClick:(UIButton *)titleButton
{
    
    
    NSInteger i = titleButton.tag;
    [UIView animateWithDuration:0.1 animations:^{
        _v_line.frame = CGRectMake(i * 100, 43, 100, 4);
    }];
    
}
- (IBAction)btn_click:(id)sender {
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    
    //设置运动时间
    animation.duration = 0.5;
    
    //设置运动type
    /*kCATransitionFade； kCATransitionPush；  kCATransitionReveal  kCATransitionMoveIn    
     （以字符串的方式赋值）@"cube";  @"suckEffect"; @"oglFlip";  @"rippleEffect";  @"pageCurl";  @"pageUnCurl";   @"cameraIrisHollowOpen";  @"cameraIrisHollowClose"; */
    animation.type = @"suckEffect";
    
    
    ///设置运动方向
    /*kCATransitionFromLeft; kCATransitionFromBottom; kCATransitionFromRight; kCATransitionFromTop */
//    animation.subtype = kCATransitionFromRight;
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    
    [self.view.layer addAnimation:animation forKey:@"animation"];
    static int i = 0;
    if (i == 0) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"02.jpg"]];
        i = 1;
    }
    else
    {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"01.jpg"]];
        i = 0;
  
    }
}

///划线绘图
- (void)drawRect {
    
    CGContextRef context = UIGraphicsGetCurrentContext(); //设置上下文
    
    //画一条线
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//线条颜色
    CGContextSetLineWidth(context, 5.0);//线条宽度
    CGContextMoveToPoint(context, 20, 20); //开始画线, x，y 为开始点的坐标
    CGContextAddLineToPoint(context, 300, 20);//画直线, x，y 为线条结束点的坐标
    CGContextStrokePath(context); //开始画线
    
}
- (void) drawRect: (CGRect) rect
{
    UIColor *currentColor = [UIColor blackColor];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
    CGContextBeginPath(context); // <---- this
    CGContextMoveToPoint(context, 50, 50);
    CGContextAddLineToPoint(context, 80, 100);
    CGContextStrokePath(context);
}
@end
